package com.system.honour.controller;

import com.system.honour.beans.PageQuery;
import com.system.honour.beans.PageResult;
import com.system.honour.common.ResultDTO;
import com.system.honour.dto.RoleDTO;
import com.system.honour.entity.SysRole;
import com.system.honour.exception.CustomizeErrorCode;
import com.system.honour.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/sys/role")
@Api(tags = "角色信息管理")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "分页查询全部角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "页码",required = true,paramType = "query",dataType ="int",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示数量",required = true,paramType = "query",dataType ="int",defaultValue = "10")
    })
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public ResultDTO page(Integer pageNo,Integer pageSize){
        PageQuery page = new PageQuery();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        PageResult result = roleService.getRoleByPage(page);
        result.setPageQuery(page);
        return ResultDTO.okOf(result.getTotal(),result.getData(),result.getPageQuery());
    }

    @ApiOperation(value = "添加角色及权限")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ResultDTO saveRole(@RequestBody RoleDTO param){
        ResultDTO resultDTO = roleService.save(param);
        return resultDTO;
    }

    @ApiOperation(value = "根据召唤师id查询对应角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "召唤师id",required = true,paramType = "query",dataType ="int")
    })
    @RequestMapping(value = "/getRole",method = RequestMethod.GET)
    public ResultDTO getRoleByUserId(Integer userId){
        ResultDTO resultDTO = roleService.getRoleByUserId(userId);
        return resultDTO;
    }

    @ApiOperation(value = "编辑角色")
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public ResultDTO updateRole(@RequestBody RoleDTO roleDTO){
        SysRole role = null;
        if (roleDTO.getId()==null){
            return ResultDTO.errorOf(CustomizeErrorCode.INCORRECT_INFORMATION);
        }
        role = roleService.getRoleById(roleDTO.getId());
        if (role!=null && !(role.getId().equals(roleDTO.getId()))){
            return ResultDTO.errorOf(CustomizeErrorCode.INCORRECT_INFORMATION);
        }
        ResultDTO resultDTO = roleService.update(roleDTO);
        return resultDTO;
    }

    @ApiOperation(value = "根据名称分页模糊搜索角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "页码",required = true,paramType = "query",dataType ="int",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示数量",required = true,paramType = "query",dataType ="int",defaultValue = "10"),
            @ApiImplicitParam(name = "name",value = "角色名",paramType = "query",dataType ="String")
    })
    @RequestMapping(value = "/vague",method = RequestMethod.POST)
    public ResultDTO getRoleByVague(String name,Integer pageNo,Integer pageSize){
        PageQuery page = new PageQuery();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        PageResult result = roleService.getRoleByVague(name,page);
        result.setPageQuery(page);
        return ResultDTO.okOf(result.getTotal(),result.getData(),result.getPageQuery());
    }

    @ApiOperation(value = "删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色id",required = true,paramType = "query",dataType ="int")
    })
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public ResultDTO deleteRole(Integer roleId){
        ResultDTO resultDTO = roleService.deleteById(roleId);
        return resultDTO;
    }
}
